﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport;
using System.IO;
using System.Collections;
using BizBrowser.RPT.PrintEntity;
using Newtonsoft.Json;

namespace BizBrowser.RPT
{
    public partial class Preview : Form
    {
        string baseDir = Path.Combine(Application.StartupPath, "reports");
        private string tempName = "";
        private string hfJson = null;
        private String detailJson = null;
        private Preview()
        {
            InitializeComponent();
        }

        public Preview(String tempName,String hfJson,String detailJson)
        {
            this.tempName = tempName;
            this.hfJson = hfJson;
            this.detailJson = detailJson;
            InitializeComponent();
            PreviewButtons pbs = this.Content.Buttons;
        }

        private void Preview_Load(object sender, EventArgs e)
        {
            FastReport.Utils.Res.LoadLocale(Application.StartupPath + "/cn.frl");
            try
            {
                var reportFile = Path.Combine(baseDir, tempName);
                report.Load(reportFile);
                List<RptHeaderFooter> data = JsonUtil.parseHeaderFooter(hfJson);
                if (data != null)
                {
                    report.RegisterData(data, Constant.H_F_NAME);
                }
                List<RptDetail> detailData = JsonUtil.parseDetail(detailJson);
                Console.WriteLine(detailData.Count);
                if (detailData != null)
                {
                    report.RegisterData(detailData, Constant.DETAIL_NAME);
                }
                report.Preview = this.Content;
                this.Content.Report.Show();
                this.TopMost = false;
            }
            catch(Exception ex)
            {
                MessageBox.Show("加载报表失败:"+ex.Message);
            }
           
        }

        public void RegisterData(IEnumerable data, String name)
        {
            report.RegisterData(data, name);
        }

        private void Content_QueryAccessibilityHelp(object sender, QueryAccessibilityHelpEventArgs e)
        {

        }

        private void report1_FinishReport(object sender, EventArgs e)
        {
           
        }

        private void Content_Click(object sender, EventArgs e)
        {
            
        }

        private void report_CustomCalc(object sender, CustomCalcEventArgs e)
        {
            
        }
    }
}
